package com.yuyou.fn.tms.service.impl;

import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.service.ISiteConfigService;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.entity.Student;
import com.yuyou.fn.educational.entity.StudentReg;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.service.IRegRuleService;
import com.yuyou.fn.educational.service.IStudentRegService;
import com.yuyou.fn.educational.service.IStudentService;
import com.yuyou.fn.tms.service.IRegService;
import com.yuyou.fn.tms.vo.CheckPermissionParam;
import com.yuyou.fn.tms.vo.CheckSignUpParam;
import com.yuyou.fn.tms.vo.TmsCheckPermissionResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class RegServiceImpl implements IRegService {

    @Resource
    private IStudentRegService studentRegService ;

    @Resource
    private ICourseClassService courseClassService ;

    @Resource
    private ISiteConfigService siteConfigService ;

    @Resource
    private IRegRuleService regRuleService ;

    @Resource
    private IStudentService studentService ;

    @Override
    public void checkSignUp(CheckSignUpParam requestParam) {

        Long studentId = requestParam.getStuId() ;

        List<Long> classIds= Utils.toLongIds(requestParam.getSubjectsClassID());
        String[] starts=requestParam.getClaStartNum().split(",");
        String[] ends=requestParam.getClaEndNum().split(",");
        Student student = studentService.findById(studentId);
        List<CourseClass> courseClassList=courseClassService.findByIds(classIds,CourseClass.BASE_FIELDS);
        Map<Long, CourseClass> classMap = BeanUtils.toMap(courseClassList, "classId");
        List<StudentReg> studentRegList=studentRegService.findReadListByClassIdsStudentId(classIds,studentId);
        Map<Long,List<StudentReg>> regMap=BeanUtils.toGroup(studentRegList,"classId");
        for (int i=0;i<classIds.size();i++){
            Long classId=classIds.get(i);
            CourseClass courseClass = classMap.get(classId);
            List<StudentReg> studentRegs=regMap.get(classId);
            int start  = Integer.parseInt(starts[i]);
            int end  = Integer.parseInt(ends[i]);
            //检查是否可以报读
            boolean r  = studentRegService.hadReg(studentRegs,start,end);
            if(r){
                throw new BusException("已经报读过"+courseClass.getClassName()+"，不可以重复报名");
            }
            //检查班级状态
            Integer lockAmount=courseClass.getLockAmount()==null?0:courseClass.getLockAmount();
            if(courseClass.getStatus() != 1 ){
                throw new BusException("所报的班级"+courseClass.getClassName()+"不是报名中");
            }

            if(courseClass.getQuota() <= lockAmount + courseClass.getRegAmount()){
                throw new BusException("所报的班级"+courseClass.getClassName()+"不够学位");
            }

            /*IRegRuleService.Result result = regRuleService.checkRegEnable(student, courseClass, true);
            if(result.getCode() != 200 ){
                throw new BusException(result.getMessage());
            }*/
        }


    }

    @Override
    public TmsCheckPermissionResult checkPermission(CheckPermissionParam requestParam) {
        Long classId = Long.valueOf(requestParam.getSubjectsClassID());
        Long studentId = requestParam.getStuId() ;

        Student student = studentService.findById(studentId);
        CourseClass courseClass = courseClassService.findById(classId);

        IRegRuleService.Result result = regRuleService.checkNeedTest(student, courseClass, true);
        if(result.getCode() == 400 ){
            return new TmsCheckPermissionResult(2,String.format(siteConfigService.getConfigValueFromCache("tms_reg_test_url") , student.getStudentId() )) ;
        }else if(result.getCode() == 401){
            return new TmsCheckPermissionResult(3,null) ;
        }
        return new TmsCheckPermissionResult(0 , null );
    }

    public static void main(String[] args) {
        System.out.println(String.format("%s" ,1l));
    }
}
